package org.osgroup.controller;

import org.osgroup.dto.EquipmentBookingRequest;
import org.osgroup.service.EquipmentBookingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 道具预约 Controller
 */
@RestController
@RequestMapping("/api/equipment")
@CrossOrigin(origins = "*")
public class EquipmentBookingController {
    
    @Autowired
    private EquipmentBookingService equipmentBookingService;
    
    /**
     * 获取可用道具列表
     */
    @GetMapping("/available")
    public ResponseEntity<Map<String, Object>> getAvailableEquipment() {
        try {
            System.out.println("🔍 收到获取道具列表请求");
            Map<String, Object> response = equipmentBookingService.getAvailableEquipment();
            System.out.println("✅ 返回道具数据: " + response);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            System.err.println("❌ 获取道具列表失败: " + e.getMessage());
            e.printStackTrace();
            return ResponseEntity.status(500).body(Map.of(
                "code", 500,
                "message", "获取道具列表失败: " + e.getMessage()
            ));
        }
    }
    
    /**
     * 预约道具
     */
    @PostMapping("/book")
    public ResponseEntity<Map<String, Object>> bookEquipment(@RequestBody EquipmentBookingRequest request) {
        try {
            Map<String, Object> response = equipmentBookingService.bookEquipment(request);
            int code = (int) response.getOrDefault("code", 200);
            
            if (code == 200) {
                return ResponseEntity.ok(response);
            } else {
                return ResponseEntity.status(code).body(response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(500).body(Map.of(
                "code", 500,
                "message", "预约失败: " + e.getMessage()
            ));
        }
    }
    
    /**
     * 获取用户预约记录
     */
    @GetMapping("/user/{userId}/bookings")
    public ResponseEntity<Map<String, Object>> getUserBookings(@PathVariable String userId) {
        try {
            Map<String, Object> response = equipmentBookingService.getUserBookings(userId);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(500).body(Map.of(
                "code", 500,
                "message", "获取预约记录失败: " + e.getMessage()
            ));
        }
    }
    
    /**
     * 取消预约
     */
    @PostMapping("/bookings/{bookingId}/cancel")
    public ResponseEntity<Map<String, Object>> cancelBooking(@PathVariable String bookingId) {
        try {
            Map<String, Object> response = equipmentBookingService.cancelBooking(bookingId);
            int code = (int) response.getOrDefault("code", 200);
            
            if (code == 200) {
                return ResponseEntity.ok(response);
            } else {
                return ResponseEntity.status(code).body(response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(500).body(Map.of(
                "code", 500,
                "message", "取消预约失败: " + e.getMessage()
            ));
        }
    }
}
